package com.ymatou.iapi.im.testcase;

import com.google.common.reflect.TypeToken;
import com.ymatou.iapi.csim.parameter.SendMessageBean;
import com.ymatou.iapi.im.parameter.GetSessionListBean;
import com.ymatou.iapi.im.parameter.SendMessageToBuyerBean;
import com.ymatou.iapi.im.parameter.SendMessageToSellerBean;
import com.ymatou.iapi.im.parameter.SessionResponse;
import com.ymatou.iapi.im.parameter.emu.MessageImportantType;
import com.ymatou.iapi.im.parameter.emu.MessageIsNewType;
import com.ymatou.iapi.im.parameter.emu.MessageReadStatus;
import com.ymatou.iapi.im.parameter.emu.ShowBroadcastSystemType;
import com.ymatou.iapi.im.service.GetSessionListCall;
import com.ymatou.iapi.prodstock.parameter.FreezeProductResult;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
*获取session列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSessionList{
	private static GetSessionListBean getsessionlistBean;
	private static GetSessionListCall getsessionlistCall;
	private static int imId = Integer.parseInt(EnvSetup.getData("userId_IM"));
	private static String imName = EnvSetup.getData("userName_IM");
	private static ImDefaultWapper im = new ImDefaultWapper();
	private static ImRedisWapper redis=new ImRedisWapper();
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productpriceSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取session列表");
	}

	@Before
	public void caseUp() throws IOException {
		getsessionlistBean=new GetSessionListBean();
		getsessionlistCall=new GetSessionListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsessionlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(String messageId) throws InterruptedException, IOException {
		Logger.verifyEquals("Success",getsessionlistCall.getString("Status"),"验证Code");
		String[] str = messageId.split("_");
		int ToUserId = Integer.parseInt(str[1]);
		int userId = Integer.parseInt(str[0]);
		Thread.sleep(5000);
		String sResult = getsessionlistCall.getJsonObject().get("Result").getAsJsonArray().toString();
		List<SessionResponse> SessionResponseList = (List<SessionResponse>) FormatUtil.GSONToObject(sResult, new TypeToken<List<SessionResponse>>(){}.getType());
		SessionResponse response = SessionResponseList.get(0);
		Logger.debug(response);
		List<YmtMessage> sYmtMessage = new ImDefaultWapper().selectImInfo(messageId);
		YmtMessage ymtMessage = sYmtMessage.get(0);
		Logger.verifyEquals(userId + "_" + ToUserId,response.getUserSessionId(),"验证UserSessionId");
		Logger.verifyEquals(response.getLatestMessage().getMessage(),ymtMessage.getMessage(),"验证Message");
		//验证时间相差不超过10秒
		AssertService.assertTime(response.getLatestMessage().getPostTime(), ymtMessage.getPostTime(), 10);
		Logger.verifyEquals(response.getToUserId(),ymtMessage.getMsgToId(),"验证ToUserId");
		String lodoUrl = new JUserCallService().GetLogoUrl(ToUserId);
		Logger.verifyEquals(response.getToUserName(),ymtMessage.getMsgTo(),"验证ToUserName");
		Logger.verifyEquals(response.getToUserLogo(),lodoUrl,"验证ToUserLogo");

		Map map = new ImDefaultWapper().selectTotalCount(userId,ToUserId).get(0);
		Logger.debug(map);
		//未读数不验证  洋管家账号没有初始化
		//Logger.verifyEquals(response.getUnReadMessageCount(),Integer.parseInt(map.get("UnReadCount").toString()),"验证未读数");
		Logger.verifyEquals(response.getMessageType().equals("Message")?1:0,Integer.valueOf(map.get("MessageType").toString()),"验证消息类型");
		Logger.verifyEquals(response.isImportant()==true?1:0,Integer.parseInt(map.get("IsImportant").toString()),"验证isimportant");
		//Logger.verifyEquals(response.getRemark().isEmpty(),map.get("Remark").toString().isEmpty(),"验证Rmark");
		Logger.verifyEquals(response.isSlient()==true?1:0,Integer.parseInt(map.get("Slient").toString()),"验证Rmark");
		Logger.verifyEquals(false,response.isTranslate(),"是否转接会话");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSessionList_001() {
		Logger.start(true,"获取session列表,买家给洋管家发送消息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendMessageBean sendMessageBean = new CsimCallService().setSendMessageBean();
			sendMessageBean.setUserId(userId);
			String messageId = new CsimCallService().setSendMessageCall(sendMessageBean).getData().getMessageId();

			getsessionlistBean.setUserId(sendMessageBean.getUserId());
			getsessionlistBean.setUnRead(MessageReadStatus.All);
			getsessionlistBean.setAppId("im.iapi.ymatou.com");
			getsessionlistBean.setShowBroadcastSystemType(ShowBroadcastSystemType.SaohuoBuyer);
			getsessionlistBean.setIsNew(MessageIsNewType.No);
			getsessionlistBean.setMessageImportantType(MessageImportantType.All);
			getsessionlistBean.setAppId("app");
			getsessionlistBean.setLongPostTime(Long.valueOf(0));
			getsessionlistBean.setCount(50);
			getsessionlistCall.setData(getsessionlistBean);
			getsessionlistCall.callService();

			verifyDB(messageId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSessionList_002() {
		Logger.start(true,"获取session列表,给买家发送消息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "nihao world";
			com.ymatou.iapi.sellercs.parameter.SendMessageBean sendMessageBean = new SellerCSIMCallService().initSendmessageBean(userId,sellerId,message);
			String messageId = new SellerCSIMCallService().SendMessage(sendMessageBean).getMessageId();

			getsessionlistBean.setUserId(sendMessageBean.getFromUserId());
			getsessionlistBean.setUnRead(MessageReadStatus.All);
			getsessionlistBean.setAppId("im.iapi.ymatou.com");
			getsessionlistBean.setShowBroadcastSystemType(ShowBroadcastSystemType.SaohuoBuyer);
			getsessionlistBean.setIsNew(MessageIsNewType.No);
			getsessionlistBean.setMessageImportantType(MessageImportantType.All);
			getsessionlistBean.setAppId("app");
			getsessionlistBean.setLongPostTime(Long.valueOf(0));
			getsessionlistBean.setCount(50);
			getsessionlistCall.setData(getsessionlistBean);
			getsessionlistCall.callService();

			verifyDB(messageId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSessionList_003() {
		Logger.start(true,"获取session列表,不合法的userid");
		try {
			getsessionlistBean.setUserId(737673);
			getsessionlistBean.setUnRead(MessageReadStatus.All);
			getsessionlistBean.setAppId("im.iapi.ymatou.com");
			getsessionlistBean.setShowBroadcastSystemType(ShowBroadcastSystemType.SaohuoBuyer);
			getsessionlistBean.setIsNew(MessageIsNewType.No);
			getsessionlistBean.setMessageImportantType(MessageImportantType.All);
			getsessionlistBean.setAppId("app");
			getsessionlistBean.setLongPostTime(Long.valueOf(0));
			getsessionlistBean.setCount(50);
			getsessionlistCall.setData(getsessionlistBean);
			getsessionlistCall.callService();
			Logger.verifyEquals(false,getsessionlistCall.getString("Result").isEmpty(),"Result为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSessionList_004() {
		Logger.start(true,"获取session列表,消息中包含导流信息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			int fromUserId = 24635724;
			SendMessageToBuyerBean sendMessageBean = ImCallService.initSendMessageToBuyerBean(fromUserId,userId,"你好，请加我微信");
			ImCallService.SendMessageToBuyer(sendMessageBean);
			getsessionlistBean.setUserId(sendMessageBean.getFromUserId());
			getsessionlistBean.setUnRead(MessageReadStatus.All);
			getsessionlistBean.setAppId("im.iapi.ymatou.com");
			getsessionlistBean.setShowBroadcastSystemType(ShowBroadcastSystemType.SaohuoBuyer);
			getsessionlistBean.setIsNew(MessageIsNewType.No);
			getsessionlistBean.setMessageImportantType(MessageImportantType.All);
			getsessionlistBean.setAppId("app");
			getsessionlistBean.setLongPostTime(Long.valueOf(0));
			getsessionlistBean.setCount(50);
			getsessionlistCall.setData(getsessionlistBean);
			getsessionlistCall.callService();
			Logger.verifyEquals("Success", getsessionlistCall.getString("Status"), "验证Status");
			String sResult = getsessionlistCall.getJsonObject().get("Result").getAsJsonArray().toString();
			List<SessionResponse> SessionResponseList = (List<SessionResponse>) FormatUtil.GSONToObject(sResult, new TypeToken<List<SessionResponse>>(){}.getType());
			SessionResponse response = SessionResponseList.get(0);
			Logger.debug(response);
			Logger.verifyEquals(true,response.getLatestMessage().getIsCancel(), "验证IsCancel");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}